#!/usr/bin/env python

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import SIGNAL,Qt

from chat import WoxxyChat

from lib.communication import WoxxySend
import os

class  ProfileWidget ( QtGui.QFrame ):
	def __init__ ( self , profile = None, parent = None,):
		super ( ProfileWidget, self ).__init__ ( parent )
		self.__profile = profile
		self.setMinimumHeight  ( 70 )
		self.setMaximumHeight  ( 70 )
		self.name_label = QtGui.QLabel  ( "no name", self )
		avatar_size = QtCore.QSize ( 50,50 )
		self.avatar = QtGui.QLabel ( self )
		self.avatar.setPixmap ( profile.avatar )
		self.avatar.setMinimumSize ( avatar_size )
		self.avatar.setMaximumSize ( avatar_size )

		#Bottoni e combo
		self.button_layout = QtGui.QHBoxLayout ()
		self.btn_secure= QtGui.QPushButton ( self )
		self.btn_secure.setText ( "" )
		icon = QtGui.QIcon()
		icon.addPixmap(QtGui.QPixmap ( ":/system/open" ), QtGui.QIcon.Normal, QtGui.QIcon.Off )
		icon.addPixmap(QtGui.QPixmap ( ":/system/close" ), QtGui.QIcon.Normal, QtGui.QIcon.On )
		self.btn_secure.setIcon ( icon )
		self.btn_secure.setCheckable ( True )
		self.btn_secure.setChecked ( False )
		self.btn_secure.setObjectName ( "btn_secure" )

		self.transfer_mode = QtGui.QComboBox ( self )
		self.transfer_mode.setObjectName ( "transfer_mode" )
		icon1 = QtGui.QIcon()
		icon1.addPixmap ( QtGui.QPixmap ( ":/system/accept" ), QtGui.QIcon.Normal, QtGui.QIcon.Off )
		self.transfer_mode.addItem ( icon1, "Accetta" )
		icon2 = QtGui.QIcon()
		icon2.addPixmap ( QtGui.QPixmap ( ":/system/askme" ), QtGui.QIcon.Normal, QtGui.QIcon.Off )
		self.transfer_mode.addItem ( icon2, "Chiedi" )
		icon3 = QtGui.QIcon()
		icon3.addPixmap ( QtGui.QPixmap ( ":/system/stop" ), QtGui.QIcon.Normal, QtGui.QIcon.Off )
		self.transfer_mode.addItem ( icon3, "Ferma" )

		self.btn_chat = QtGui.QPushButton ( self )
		self.btn_chat.setText ( "" )
		icon4 = QtGui.QIcon()
		icon4.addPixmap ( QtGui.QPixmap ( ":/system/chat" ), QtGui.QIcon.Normal, QtGui.QIcon.Off )
		self.btn_chat.setIcon ( icon4 )
		self.btn_chat.setIconSize ( QtCore.QSize ( 20, 20 ) )
		self.btn_chat.setObjectName ( "btn_chat" )

		spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
		self.button_layout.addWidget ( self.btn_secure )
		self.button_layout.addWidget ( self.transfer_mode)
		self.button_layout.addWidget ( self.btn_chat )
		self.button_layout.addItem ( spacerItem )



		self.main_layout  = QtGui.QGridLayout ( self )
		self.main_layout.addWidget ( self.avatar, 0, 0, 2, 1)
		self.main_layout.addWidget ( self.name_label, 0 , 1, 1, 1 )
		self.main_layout.addLayout ( self.button_layout, 1, 1, 1, 1)

		self.setAcceptDrops  ( True )
		self.parent = parent
		if self.__profile :
			self.name_label.setText ( self.__profile.name )

		self.chat = WoxxyChat()

		#Events
		self.connect ( self.btn_secure, SIGNAL ( "toggled(bool)" ), self.__setsecure )
		self.connect ( self.transfer_mode, SIGNAL ( "currentIndexChanged(int)" ) , self.__settransfer )
		self.connect ( self.btn_chat, SIGNAL ( "clicked()" ) , self.__openchat )



	def dropEvent(self, e):
		self.setStyleSheet  ( "")
		self.__send ( e.mimeData() )

	def __send ( self, data ):
		dest_ip = self.__profile.ip
		secure = self.__profile.secure
		for url in  data.urls():
			print "SEND: ", dest_ip, " - ", url.toLocalFile(), self.__profile
			ws = WoxxySend ()
			#ws.put_socket ( dest_ip, 12345, str (  url.toLocalFile() ) )
			ws.send( dest_ip, 12345, str (  url.toLocalFile() ), secure )

			self.parent.emit ( SIGNAL ( "add_transfer" ) , str( url.toLocalFile() ), self.__profile.name )

	def dragEnterEvent(self, e):
		self.setStyleSheet  ( "background-color: rgb(205, 210, 255);")
		e.accept()

	def dragLeaveEvent(self, e):
		self.setStyleSheet  ( "")
		e.accept()

	def setProfile ( self, profile ):
		self.__profile = profile
		self.name_label.setText ( self.__profile.name )

	def profile ( self ):
		return self.__profile

	def __setsecure ( self , checked ):
		self.__profile.secure = checked

	def __settransfer ( self, val ):
		self.__profile.tranfer_mode = val

	def __openchat ( self ):
		msg = WoxxyMessage( self.__profile )
		msg.exec_()
		#self.chat.show()


class SelectAvatar ( QtGui.QLabel ):
	def __init__ ( self, parent = None, image_path = '' ):
		super ( SelectAvatar, self ).__init__ ( parent )
		size = QtCore.QSize ( 50, 50 )
		self.setMinimumSize ( size )
		self.setMaximumSize ( size )
		self.extensions = [ '.png', '.jpg' ]
		self.set_avatar ( image_path )


	def mouseReleaseEvent ( self, ev ):
		file_source = QtGui.QFileDialog.getOpenFileName(self, "Apri Immagine", filter ="Immagini (*.png *.jpg)" );
		if file_source :
			file_ext = os.path.splitext ( str ( file_source ) )
			print file_ext
			if not file_ext [ 1 ] in self.extensions:
				#messaggio estensione non valida
				print "non faccio nulla"
				return
			print "carico immagine %s" , file_source
			self.set_avatar ( file_source )

	def set_avatar ( self, image_path ):
		self.path = image_path
		self.avatar = QtGui.QPixmap( ':/fiend/avatar' )
		if self.path :
			self.avatar.load ( image_path )
			self.avatar.scaled ( 50, 50 )
		self.setPixmap ( self.avatar )

class  TransfertWidget ( QtGui.QFrame ):
	def __init__ ( self ,parent = None, file_name = '', sender = ''):
		super ( TransfertWidget, self ).__init__ ( parent )
		self.parent = parent
		self.__setup_ui ( file_name, sender )


	def __setup_ui ( self, file_name, sender ):
		self.setMinimumHeight  ( 35 )
		self.setMaximumHeight  ( 35 )
		self.lbl_file = QtGui.QLabel ( file_name , self )
		self.lbl_profilo = QtGui.QLabel ( sender, self )
		self.progress = QtGui.QProgressBar ( self )
		self.btn_remove = QtGui.QPushButton ( self )
		self.btn_remove.setIcon ( QtGui.QIcon ( QtGui.QPixmap( ':/system/remove' ) ) )

		self.layout = QtGui.QHBoxLayout ( self )
		self.layout.addWidget ( self.lbl_profilo)
		self.layout.addWidget ( self.lbl_file )
		self.layout.addWidget ( self.progress )
		self.layout.addWidget ( self.btn_remove )

		self.connect ( self.btn_remove , SIGNAL ( "clicked()" ), self.remove_file )

	def remove_file ( self ):
		self.parent.emit ( SIGNAL ( "remove_transfer" ) , str ( self.lbl_file.text() ) )
		self.close()



class WoxxyMessage ( QtGui.QDialog ):
	def __init__( self, fname, size, secure ):
		QtGui.QDialog.__init__(self )
		self.setWindowFlags( Qt.FramelessWindowHint )
		self.setAttribute ( Qt.WA_TranslucentBackground )

		self.data = { "fname" : fname, "size" : size, "secure" : secure }

		self.setMouseTracking ( True )
		self.btn_close = QtCore.QRect ( 20, 50, 80, 20 )
		self.btn_close_shadow = QtCore.QRect ( 21, 51, 80, 20 )
		self.btn_accept = QtCore.QRect ( 200, 50, 80, 20 )
		self.btn_accept_shadow = QtCore.QRect ( 201, 51, 80, 20 )

	def paintEvent(self, event):
		screen = QtGui.QDesktopWidget().screenGeometry()
		x = screen.width() - 320
		y = 50
		font_text = QtGui.QFont()
		font_metric = QtGui.QFontMetrics ( font_text )
		self.setGeometry ( x ,y ,300,80)
		painter = QtGui.QPainter()
		painter.begin(self)
		painter.setRenderHint ( QtGui.QPainter.Antialiasing )
		painter.setBrush ( QtGui.QColor ( 0, 0, 0, 200 ) )
		painter.setPen ( Qt.NoPen )
		w = self.geometry().width()
		h = self.geometry().height()

		rect = QtCore.QRect ( 1,1,w-2,h-2 )
		painter.drawRoundedRect (rect ,10, 10 )

		painter.setPen ( QtGui.QColor ( 255,255,255, 255 ) )
		rect_txt = QtCore.QRect ( 0,0, w , font_metric.height() )
		painter.drawText(rect_txt, Qt.AlignCenter, "Richiesta Invio file" )
		rect_txt = QtCore.QRect ( 0 ,font_metric.height() , w , font_metric.height() )
		painter.drawText ( rect_txt, Qt.AlignCenter,  self.data [ 'fname' ] )

		#button
		painter.setPen ( Qt.NoPen )
		font_text.setBold ( True )
		self.setFont ( font_text )
		gradient = QtGui.QLinearGradient ( QtCore.QPointF(0, 0), QtCore.QPointF(0, 100))
		#gradient.setColorAt ( 0.6, QtGui.QColor ( 241, 102, 63, 255 ) )
		gradient.setColorAt ( 0.2, QtGui.QColor ( 199, 0, 23, 255 ) )
		gradient.setColorAt ( 1, QtGui.QColor ( 241, 102, 63, 255 ) )
		#radient.setColorAt ( 0, Qt.black )

		painter.setBrush ( QtGui.QBrush ( gradient ) )
		painter.drawRoundedRect ( self.btn_close, 10 , 10 )

		painter.setPen ( Qt.gray )
		painter.drawText (self.btn_close_shadow, Qt.AlignCenter, "Rifiuta" )
		painter.setPen ( Qt.white )
		painter.drawText ( self.btn_close, Qt.AlignCenter, "Rifiuta" )


		painter.setPen ( Qt.NoPen )
		gradient.setColorAt ( 0.2, QtGui.QColor ( 152, 210, 36, 255 ) )
		gradient.setColorAt ( 1, QtGui.QColor ( 60, 154, 0, 255 ) )
		painter.setBrush ( QtGui.QBrush ( gradient ) )
		painter.drawRoundedRect ( self.btn_accept, 10 , 10 )


		painter.setPen ( Qt.gray )
		painter.drawText (self.btn_accept_shadow, Qt.AlignCenter, "Accetta" )
		painter.setPen ( Qt.white )
		painter.drawText ( self.btn_accept, Qt.AlignCenter, "Accetta" )

		painter.end()

	def mouseMoveEvent ( self, event ) :
		self.setCursor ( Qt.ArrowCursor )
		if self.btn_close.contains ( event.pos() ):
			self.setCursor ( QtGui.QCursor ( Qt.PointingHandCursor ) )
		if self.btn_accept.contains ( event.pos() ):
			self.setCursor ( QtGui.QCursor ( Qt.PointingHandCursor ) )

	def mousePressEvent ( self, event ):
		if self.btn_close.contains ( event.pos() ) :
			self.reject()
		if self.btn_accept.contains ( event.pos() ):
			self.accept()


